using System;
using System.Windows.Forms;

namespace Minesweeper02
{
	/// <summary>
	/// Summary description for Handler.
	/// </summary>
	public enum EventList
	{
		//
		LeftDown,
		//
		BothDown,
		//
		RightClick,
		//
		LeftClick,
		//when both of two buttons click
		BothClick,
		//
		LeftMove,
		//
		BothMove,
		//
		LeftOut,
		//
		BothOut
	}
	public class ImgEventArgs:EventArgs
	{
		public int X;
		public int Y;
		public EventList Event;
		public ImgEventArgs()
		{
			this.X=0;
			this.Y=0;
			this.Event=0;
		}
		public void Active(int _X,int _Y,EventList _Event)
		{
			this.X=_X;
			this.Y=_Y;
			this.Event=_Event;
		}
	}
	public class ImgMouseEventHandler
	{
		private bool _LeftDown;
		private bool _RightDown;
		private bool _OtherDown;
		private bool _Out;
		private bool _Clicked;
		private int columns;
		private int rows;
		private int _ItemWeight;
		private int _ItemHeight;
		private int MX;
		private int MY;
		private ImgEventArgs ie;
		public delegate void ImgEventHandler(object sender,ImgEventArgs iea);
		public event ImgEventHandler IEvent;
		//constructor
		public ImgMouseEventHandler(int _columns,int _rows,int _w,int _h)
		{
			//
			// TODO: Add constructor logic here
			//
            ResetAttribute();
	        columns=_columns;
			rows=_rows;
			InitSizeItem(_w,_h);
			ie=new ImgEventArgs();
		}
		public void ResetAttribute(){
			MX=0;
			MY=0;
			_LeftDown=false;
			_RightDown=false;
			_Clicked=false;
			_OtherDown=false;
			_Out=false;

		}
		public void InitSizeItem(int _w,int _h)
		{
			_ItemWeight=_w;
			_ItemHeight=_h;
		}
		private void ActiveEvent(object sender,EventList _Event)
		{
			if (!_Out)
			{
				ie.Active(MX,MY,_Event);
				IEvent(sender,ie);
			}
		}
		public void MouseDown(object sender,MouseEventArgs me)
		{
			switch (me.Button)
			{
				case MouseButtons.Left:
					_LeftDown=true;
					if (_OtherDown)
					{
					   _Clicked=true;
						ActiveEvent(sender,EventList.BothDown);
					}
					else
					{
						if (_RightDown)
						{
							_Clicked=true;
							ActiveEvent(sender,EventList.BothDown);
						}
						else
						{
							ActiveEvent(sender,EventList.LeftDown);
						}
					}
					break;
				case MouseButtons.Right:
					_RightDown=true;
					if (_OtherDown){
						_Clicked=true;
						ActiveEvent(sender,EventList.BothDown);
					}
					else
					{
						if (_LeftDown)
						{
							_Clicked=true;
							ActiveEvent(sender,EventList.BothDown);
						}
						else
						{
							if (!_Clicked)
							{
								ActiveEvent(sender,EventList.RightClick);
							}
						}
					}
					break;
				default:
					_OtherDown=true;
					_Clicked=true;
					ActiveEvent(sender,EventList.BothDown);
               		break;
			}

		}
		public void MouseUp(object sender,MouseEventArgs me)
		{
			switch (me.Button)
			{
				case MouseButtons.Left:

					_LeftDown=false;
					if (_OtherDown)
					{
						ActiveEvent(sender,EventList.BothClick);
					}
					else
					{
					    if (_RightDown)
					    {
							ActiveEvent(sender,EventList.BothClick);
					    }
					    else
					    {
							if(!_Clicked)
							{
								ActiveEvent(sender,EventList.LeftClick);
							}
							else
							{
								_Clicked=false;
								ActiveEvent(sender,EventList.LeftOut);
							}
						}
				    }
					break;
				case MouseButtons.Right:
					_RightDown=false;
					if (_OtherDown)
					{
						ActiveEvent(sender,EventList.BothClick);
					}
					else
					{	
						if (_LeftDown)
						{
							ActiveEvent(sender,EventList.BothClick);
						}
						else
						{
							_Clicked=false;
						}
					}
					break;
				default:
                    _OtherDown=false;  						
					if (!_LeftDown && !_RightDown)
					{
						_Clicked=false;
						ActiveEvent(sender,EventList.BothClick);
					}
					if (_LeftDown && _RightDown){
						ActiveEvent(sender,EventList.BothClick);
					}
					break;
			}

		}
		public void MouseMove(object sender,MouseEventArgs me)
		{
			if((me.X>=(MX+1)*_ItemWeight) || (me.X<MX*_ItemWeight)||(me.Y>=(MY+1)*_ItemHeight)||(me.Y<MY*_ItemHeight))
			{
				if ((_LeftDown && _RightDown) || _OtherDown)
				{
					ActiveEvent(sender,EventList.BothOut);
				}
				if (_LeftDown && !_OtherDown && !_Clicked)
				{
					ActiveEvent(sender,EventList.LeftOut);
				}
				
				int x=(int)(me.X/_ItemWeight);
				int y=(int)(me.Y/_ItemHeight);
				if (x>=0 && x<columns && y>=0 && y<rows &&(x!=MX || y!=MY))
				{
					_Out=false;
					MX=x;
					MY=y;
					if ((_LeftDown && _RightDown) || _OtherDown)
					{
						ActiveEvent(sender,EventList.BothMove);
					}
					if (_LeftDown && !_OtherDown && !_Clicked)
					{
						ActiveEvent(sender,EventList.LeftMove);
					}
				}
				else
				{
				    _Out=true;
				}
			}
		}
	}
}
